package com.hxzy.controller;

import com.hxzy.pojo.Result;
import com.hxzy.utils.AliyunOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author wzm
 * @create 2024-03-06 11:38
 * project_name:JavaWeb2401
 * @Description
 */
@Slf4j
@RestController
public class UploadController {
    //注入阿里云oss工具对象
    @Autowired
    private AliyunOssUtil aliyunOssUtil;


    //处理普通表单文件上传的方法
   /* @PostMapping("/upload")
    public Result upload(String username, Integer age,MultipartFile image) throws IOException {
        log.info("文件上传的表单数据为：{}，{}，{}",username,age,image);

        //获取原始文件名
        String fileName = image.getOriginalFilename();//1.jpg

        //截取后缀名
        String lastName = fileName.substring(fileName.lastIndexOf("."));//.jpg

        //为了防止同名文件会覆盖之前的内容需要保证文件名唯一  UUID
        fileName = UUID.randomUUID().toString()+lastName;//asfasdf-ddddv+.jpg


        log.info("文件名{}",fileName);
        //将上传时的临时文件转移到磁盘中存储起来
        image.transferTo(new File("D:/images/"+fileName));


        return Result.success();
    }*/

    //上传文件到阿里云oss
    @PostMapping("/upload")
    public Result  upload(MultipartFile image) throws IOException {
        //上传文件到阿里云oss
        String url = aliyunOssUtil.putOss(image);
        //返回上传成功后的url地址给前端展示缩略图
        return Result.success(url);
    }
}
